<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Message extends CI_Controller {

	public function index()
	{
		is_login($this->session);
		
		$all_message_type = $this->config->config['BASE_MESSAGE_TYPE'];
		$data = array(
			'version' => $this->config->config['QUOTE_VERSION'],
			'all_message_type' => $all_message_type
		);
		
		$keyword = getdata($this->input->get('keyword'));
		$message_type = getdata($this->input->get('message_type'));
		$time_start = getdata($this->input->get('time_start'));
		$time_end = getdata($this->input->get('time_end'));
		$userid = getdata($this->input->get('userid'));
		$data['search_item'] = array(
			'keyword' => $keyword,
			'message_type' => $message_type,
			'time_start' => $time_start,
			'time_end' => $time_end,
			'userid' => $userid
		);
		
		$where_arr = $cond = array();
		if ($keyword){
			$where_arr[] = 'content like "%'.$keyword.'%"';
		}
		if ($message_type){
			$where_arr[] = 'type=?';
			$cond[] = $message_type;
		}
		if ($time_start){
			$where_arr[] = 'add_time>=?';
			$cond[] = $time_start . ' 00:00:00';
		}
		if ($time_end){
			$where_arr[] = 'add_time<=?';
			$cond[] = $time_end . ' 23:59:59';
		}
		if ($userid){
			$where_arr[] = 'from_user=?';
			$cond[] = $userid;
		}
		$where = implode(' and ', $where_arr);
				
		$this->load->model('Message_model', 'Mmessage');
		$re_list = $this->Mmessage->get_list($where, $cond);
		foreach ($re_list as &$item){
			$item['type_str'] = $all_message_type[$item['type']];
		}
		$data['message_list'] = $re_list;
		
		$this->load->view('message', $data);
		
	}
	
}
